package com.briup.day29;

import java.io.*;
import java.net.Socket;
import java.util.Scanner;

/**
 * @author ThinkBook
 */
public class ReversalClient {
    public static void main(String[] args) throws IOException {

        //创建一个socket对象
        Socket socket = new Socket("127.0.0.1",8989);
        System.out.println("成功连接到服务器，socket："+socket);

        //创建输入输出流
        InputStream is = socket.getInputStream();
        OutputStream os = socket.getOutputStream();

        //创建打印流 写
        PrintStream ps = new PrintStream(os);
        //创建缓冲流 读取
        Reader rs = new InputStreamReader(is);
        BufferedReader br = new BufferedReader(rs);
        //3.核心业务：数据传输
        // 从键盘录入整行数据 发送给服务器，遇到quit结束录入
        Scanner sc = new Scanner(System.in);
        System.out.println("请发送录入的数据：");
        String line ;
        while (true){
            line = sc.nextLine();
            //发送给服务器
            ps.println(line);
            System.out.println("发送数据成功");
            if ("quit".equals(line)){
                break;
            }
            //从服务器接收 返回的信息(反转字符串)
            String message = br.readLine();
            System.out.println("接收的信息："+message);
        }
        //关闭资源
        ps.close();
        br.close();
        socket.close();

    }
}
